package org.jlada.chassis.adapter;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.jlada.chassis.ChassisTypes;
import org.jlada.chassis.exception.UnabletoFindChassisException;

public class ChassisAdapter {

	private InputStream is;
	private byte[] type;
	private int ord;
	
	public ChassisAdapter(){
		type = new byte[2];
	}
		
	public void findChassisType() throws UnabletoFindChassisException{
		try {
			Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\" +"wscript.exe chassis.vbs");
			is = new FileInputStream("ch001.txt");
			is.read(type, 0, 2);
			String s = new String(type);
			ord =  Integer.parseInt(s);
			ord--;
		} catch (Exception e) {
			ord = 25;
			throw new UnabletoFindChassisException(e.getMessage());
		}
		finally{
			try {
				if(is != null)
					is.close();
			} catch (IOException e) {
				throw new UnabletoFindChassisException(e.getMessage());
			}
		}
	}
	
	public ChassisTypes getChassisType() {
		switch(ord){
		case 0:
			return ChassisTypes.Other;
		case 1:
			return ChassisTypes.Unknown;
		case 2:
			return ChassisTypes.Desktop;
		case 3:
			return ChassisTypes.Low_Profile_Desktop;
		case 4:
			return ChassisTypes.Pizza_Box;
		case 5:
			return ChassisTypes.Mini_Tower;
		case 6:
			return ChassisTypes.Tower;
		case 7:
			return ChassisTypes.Portable;
		case 8:
			return ChassisTypes.Laptop;
		case 9:
			return ChassisTypes.Notebook;
		case 10:
			return ChassisTypes.Handheld;
		case 11:
			return ChassisTypes.Docking_Station;
		case 12:
			return ChassisTypes.All__in__One;
		case 13:
			return ChassisTypes.Sub__Notebook;
		case 14:
			return ChassisTypes.Space_Saving;
		case 15:
			return ChassisTypes.Lunch_Box;
		case 16:
			return ChassisTypes.Main_System_Chassis;
		case 17:
			return ChassisTypes.Expansion_Chassis;
		case 18:
			return ChassisTypes.Sub__Chassis;
		case 19:
			return ChassisTypes.Bus_Expansion_Chassis;
		case 20:
			return ChassisTypes.Peripheral_Chassis;
		case 21:
			return ChassisTypes.Storage_Chassis;
		case 22:
			return ChassisTypes.Rack_Mount_Chassis;
		case 23:
			return ChassisTypes.Sealed__Case_PC;
		default:	
			return ChassisTypes._Unknown;
		}
	}

}
